// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.network.packets;

import java.nio.ByteBuffer;
import zombie.core.network.ByteBufferWriter;
import zombie.core.raknet.UdpConnection;
import zombie.debug.DebugLog;
import zombie.vehicles.BaseVehicle;
import zombie.vehicles.VehicleManager;

public class VehicleAuthorizationPacket implements INetworkPacket {
    short vehicleId = -1;
    BaseVehicle.Authorization authorization = BaseVehicle.Authorization.Server;
    short authorizationPlayer = -1;

    public void set(BaseVehicle vehicle, UdpConnection connection) {
        BaseVehicle.ServerVehicleState serverVehicleState = vehicle.connectionState[connection.index];
        serverVehicleState.setAuthorization(vehicle);
        this.authorization = vehicle.netPlayerAuthorization;
        this.authorizationPlayer = vehicle.netPlayerId;
        this.vehicleId = vehicle.getId();
    }

    public void process() {
        BaseVehicle baseVehicle = VehicleManager.instance.getVehicleByID(this.vehicleId);
        if (baseVehicle != null) {
            DebugLog.Vehicle
                .trace("vehicle=%d netPlayerAuthorization=%s netPlayerId=%d", baseVehicle.getId(), this.authorization.name(), this.authorizationPlayer);
            baseVehicle.netPlayerFromServerUpdate(this.authorization, this.authorizationPlayer);
        }
    }

    @Override
    public void parse(ByteBuffer b, UdpConnection connection) {
        this.vehicleId = b.getShort();
        this.authorization = BaseVehicle.Authorization.valueOf(b.get());
        this.authorizationPlayer = b.getShort();
    }

    @Override
    public void write(ByteBufferWriter b) {
        b.putShort(this.vehicleId);
        b.putByte(this.authorization.index());
        b.putShort(this.authorizationPlayer);
    }

    @Override
    public String getDescription() {
        return null;
    }
}
